package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MedicinePlaceMapper;
import com.ruoyi.system.domain.MedicinePlace;
import com.ruoyi.system.service.IMedicinePlaceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 中药登记场所详请Service业务层处理
 *
 * @author yanhe
 * @date 2025-04-16
 */
@Service
public class MedicinePlaceServiceImpl implements IMedicinePlaceService {
    @Autowired
    private MedicinePlaceMapper medicinePlaceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchInsertMedicinePlace(List<MedicinePlace> dataList) {
        if (StringUtils.isNull(dataList) || dataList.size() == 0) {
            throw new ServiceException("导入中药库场所数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MedicinePlace data : dataList) {
            try {
                medicinePlaceMapper.insertMedicinePlace(data);
                successNum++;
                successMsg.append("<br/>" + successNum + "、场所 " + data.getRegisterCode() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、场所 " + data.getRegisterCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询中药登记场所详请
     *
     * @param id 中药登记场所详请主键
     * @return 中药登记场所详请
     */
    @Override
    public MedicinePlace selectMedicinePlaceById(Integer id) {
        return medicinePlaceMapper.selectMedicinePlaceById(id);
    }

    /**
     * 查询中药登记场所详请列表
     *
     * @param medicinePlace 中药登记场所详请
     * @return 中药登记场所详请
     */
    @Override
    public List<MedicinePlace> selectMedicinePlaceList(MedicinePlace medicinePlace) {
        return medicinePlaceMapper.selectMedicinePlaceList(medicinePlace);
    }

    /**
     * 新增中药登记场所详请
     *
     * @param medicinePlace 中药登记场所详请
     * @return 结果
     */
    @Override
    public int insertMedicinePlace(MedicinePlace medicinePlace) {
        medicinePlace.setCreateTime(DateUtils.getNowDate());
        return medicinePlaceMapper.insertMedicinePlace(medicinePlace);
    }

    /**
     * 修改中药登记场所详请
     *
     * @param medicinePlace 中药登记场所详请
     * @return 结果
     */
    @Override
    public int updateMedicinePlace(MedicinePlace medicinePlace) {
        medicinePlace.setUpdateTime(DateUtils.getNowDate());
        return medicinePlaceMapper.updateMedicinePlace(medicinePlace);
    }

    /**
     * 批量删除中药登记场所详请
     *
     * @param ids 需要删除的中药登记场所详请主键
     * @return 结果
     */
    @Override
    public int deleteMedicinePlaceByIds(Integer[] ids) {
        return medicinePlaceMapper.deleteMedicinePlaceByIds(ids);
    }

    /**
     * 删除中药登记场所详请信息
     *
     * @param id 中药登记场所详请主键
     * @return 结果
     */
    @Override
    public int deleteMedicinePlaceById(Integer id) {
        return medicinePlaceMapper.deleteMedicinePlaceById(id);
    }
}
